using System;
using System.Windows;
using System.Windows.Data;

#if SL
using System.Windows.Threading;
using UIPropertyMetadata = System.Windows.PropertyMetadata;
#endif

namespace Comarch.Animation
{
    //TODO: update this class
    internal static class DependencyObjectExtensions
    {
        private class DummyDependencyObject : DependencyObject {}

        private static readonly DependencyProperty DummyProperty = DependencyProperty.RegisterAttached(
            "Dummy",
            typeof(Object),
            typeof(DependencyObject),
            new UIPropertyMetadata(null));

        private static readonly DummyDependencyObject _object = new DummyDependencyObject();

        public static void SetValue(object container, string dependencyProperty, object value)
        {
            var binding = new Binding(dependencyProperty) { Source = container, Mode = BindingMode.TwoWay };
            BindingOperations.SetBinding(_object, DummyProperty, binding);
            _object.SetValue(DummyProperty, value);
#if SL
            _object.SetValue(DummyProperty, DependencyProperty.UnsetValue);
#elif WPF
            BindingOperations.ClearBinding(_object, DummyProperty);
#endif
        }

        public static object GetValue(this DependencyObject container, string dependencyProperty)
        {
            var binding = new Binding(dependencyProperty) { Source = container };
            BindingOperations.SetBinding(_object, DummyProperty, binding);

            return _object.GetValue(DummyProperty);
        }
    }
}